﻿using CKS.PLC.Core.Reader;
using CKS.Schedule;
using Infrastructure;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Infrastructure;
using ZR.ServiceCore.Signalr;

namespace CKS.PLC.Core.CronJob
{
    /// <summary>
    /// 1.监控各个设备连接情况，自动重连
    /// </summary>
    [Schedule]
    public class MonitorClientJob : CronJobBase
    {
        public SocketReaderClient Client1 { get; set; }
        public SocketReaderClient Client2 { get; set; }
        public  SocketReaderClient Client3 { get; set; }

        public WssPrintClient WssPrintClient { get; set; }
        public MonitorClientJob() : base("监视扫码器", "0/1 * * * * ?")
        {

            //从配置文件读取摄像头
           var reader1 = AppSettings.Get<CodeReaderConfig>("CodeReader1");
           var reader2 = AppSettings.Get<CodeReaderConfig>("CodeReader2");
           var reader3 = AppSettings.Get<CodeReaderConfig>("CodeReader3");
            var printServerurl = AppSettings.GetConfig("PrintServer");
            
            Client1 = new SocketReaderClient().SetIp(reader1.Ip).SetPort(reader1.Port).SetTimeOut(reader1.TimeOut);
            Client2 = new SocketReaderClient().SetIp(reader2.Ip).SetPort(reader2.Port).SetTimeOut(reader2.TimeOut);
            Client3 = new SocketReaderClient().SetIp(reader3.Ip).SetPort(reader3.Port).SetTimeOut(reader3.TimeOut);
            WssPrintClient = new WssPrintClient(printServerurl);
            Client1.IniSocketConnect();
            Client2.IniSocketConnect();
            Client3.IniSocketConnect();
            WssPrintClient.ConnectAsync().ConfigureAwait(false).GetAwaiter().GetResult();

        }

        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            if(!Client1.IsConnected)
                Client1.IniSocketConnect();
            if (!Client2.IsConnected)
                Client2.IniSocketConnect();
            if (!Client3.IsConnected)
                Client3.IniSocketConnect();
            if (!WssPrintClient.IsConnected)
                await WssPrintClient.ConnectAsync();
            await Task.CompletedTask;
        }





    }

    public class CodeReaderConfig
    {
        public string Ip { get; set; }
        public int Port { get; set; }
        public int TimeOut { get; set; }
    }
}
